package com.hdq.controller.admin;

import com.hdq.constant.JwtClaimsConstant;
import com.hdq.context.BaseContext;
import com.hdq.result.PageResult;
import com.hdq.result.Result;
import com.hdq.service.ManagerService;
import dto.ManagerAddDTO;
import dto.ManagerFilterFormDTO;
import dto.ManagerLoginDTO;
import dto.ManagerEditDTO;
import entity.Manager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import vo.ManagerInfoVO;
import vo.ManagerLoginVO;
import vo.ManagerXiaLalistVO;

import java.util.List;

@RestController
@RequestMapping("/admin/manager")
@Tag(name = "管理员管理")
@Slf4j
public class ManagerController {

    @Autowired
    private ManagerService managerService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 管理员登录
     *
     * @param managerLoginDTO
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result<ManagerLoginVO> login(@RequestBody ManagerLoginDTO managerLoginDTO) {
        String token = managerService.managerLogin(managerLoginDTO);
        ManagerLoginVO managerLoginVO = ManagerLoginVO.builder().token(token).build();
        return Result.success(managerLoginVO);
    }

    /**
     * 管理员退出登录
     * @return
     */
    @PostMapping("/logout")
    @Operation(summary = "退出登录")
    public Result logout() {
        log.info("管理员退出登录");
//        删除redis的token
        redisTemplate.opsForHash().delete(JwtClaimsConstant.TOKEN,BaseContext.getCurrentId());
        return Result.success();
    }

    /**
     * 获取管理员信息和权限菜单
     * @return
     */
    @GetMapping ("/getInfo")
    @Operation(summary = "获取管理员信息和权限菜单")
    public Result getInfo() {
        ManagerInfoVO managerInfoVO= managerService.getInfo();
        return Result.success(managerInfoVO);
    }

    @PostMapping ("/{page}")
    @Operation(summary = "查询管理员列表")
    public Result<PageResult> selectByPage(@PathVariable Integer page, @RequestParam Integer pageSize , @RequestBody(required = false) ManagerFilterFormDTO managerFilterFormDTO) {
        Manager manager = new Manager();
        if(managerFilterFormDTO!=null) BeanUtils.copyProperties(managerFilterFormDTO,manager);
        PageResult list= managerService.selectByPage(page,pageSize,manager);
        return Result.success(list);
    }
    @PostMapping
    @Operation(summary = "新增管理员")
    public Result insert(@RequestBody ManagerAddDTO managerAddDTO) {
        managerService.insert(managerAddDTO);
        return Result.success();
    }


    @PutMapping("/{id}")
    @Operation(summary = "编辑管理员")
    public Result update(@PathVariable Long id, @RequestBody ManagerEditDTO managerEditDTO) {
        managerService.update(id, managerEditDTO);
        return Result.success();
    }

    @DeleteMapping
    @Operation(summary = "删除管理员/批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        managerService.delete(ids);
        return Result.success();
    }

    @PutMapping("/banned")
    @Operation(summary = "封禁管理员")
    public Result banned(@RequestParam List<Long> ids , @RequestParam Integer status) {
        managerService.banned(ids,status);
        return Result.success();
    }

    @GetMapping("/selectXiaLa")
    @Operation(summary = "查询管理员下拉列表")
    public Result selectXiaLa() {
        List<ManagerXiaLalistVO> list= managerService.selectXiaLa();
        return Result.success(list);
    }

}
